package com.shop2cn.iapi.agentproducts.testcase;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.apache.http.client.ClientProtocolException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.agentproducts.parameter.AddAgentProductBean;
import com.shop2cn.iapi.agentproducts.parameter.CatalogDto;
import com.shop2cn.iapi.agentproducts.parameter.ImportAgentProductBean;
import com.shop2cn.iapi.agentproducts.parameter.ProductDetailsDto;
import com.shop2cn.iapi.agentproducts.parameter.ProductListDto;
import com.shop2cn.iapi.agentproducts.parameter.UpdateProductSkuBean;
import com.shop2cn.iapi.agentproducts.service.UpdateProductSkuCall;
import com.ymatou.iapi.godmch.parameter.createDepotBean;
import com.ymatou.iapi.godmch.parameter.setConditionMarkupConfigBean;
import com.ymatou.iapi.productsynces.parameter.es.EsGodProduct;
import com.ymatou.mq.admin.parameter.MessageListBean;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AddAgentProductCallService;
import com.ymttest.business.service.AgentProductCallService;
import com.ymttest.business.service.MqQueryCallService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.YmtCatalogsI;
import com.ymttest.database.model.godmch.mch_depot_info;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.utils.AgentUsers;
import com.ymttest.utils.TestDataManager;

/**
 * 更新商品skuTest FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_UpdateProductSku {
	private static UpdateProductSkuBean updateproductskuBean;
	private static UpdateProductSkuCall updateproductskuCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("更新商品skuTest");
	}

	@Before
	public void caseUp() {
		updateproductskuBean = new UpdateProductSkuBean();
		updateproductskuCall = new UpdateProductSkuCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(updateproductskuCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	// vip渠道
	private static int vipRootUserId = AgentUsers.vipRootUserId;
	private static String vipRootUserName = "NickautoTest";
	private static int csId = AgentUsers.csId;
	// vip渠道->代理
	private static int vipAgentUserId1 = AgentUsers.vipAgentUserId1;
	private static String vipAgentUserName1 = "dgsq408897016";
	private static int csId1 = AgentUsers.csId1;
	// vip渠道->代理->代理
	private static int vipAgentUserId2 = AgentUsers.vipAgentUserId2;
	private static String vipAgentUserName2 = "NickautoTest";

	
	public void verifyMongoCurMq(String pid) throws ClientProtocolException, IOException, InterruptedException {
		Thread.sleep(5000);
		MessageListBean MessageListBean2 = MqQueryCallService.initAgentMongoCrud(pid);
		List<Map> joa2 = MqQueryCallService.GetMqMessage(MessageListBean2);
		Logger.debug(joa2);
		int redo = 0;
		while (joa2.size() < 2 && redo < 3) {
			Thread.sleep(5000);
			joa2 = MqQueryCallService.GetMqMessage(MessageListBean2);
			redo++;
		}
		String actionType = "ModifyAgentProduct";
		String appId = "agentproducts.iapi.ymatou.com";
		Logger.verifyEquals(actionType, joa2.get(0).get("ActionType"), "验证ActionType");
		Logger.verifyEquals(pid, joa2.get(0).get("ProductId"), "验证ProductId");
		Logger.verifyEquals(appId, joa2.get(0).get("AppId"), "验证AppId");
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_UpdateProductSku_001() {
		Logger.start(true, "修改数据，成功");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(vipRootUserId,
					vipRootUserName, false);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			System.out.println(addAgentProductBean.getCatalogList().get(0).getStock());
			Thread.sleep(2000);
			updateproductskuBean.setUserId(vipRootUserId);
			updateproductskuBean.setProductId(productId);
			List<CatalogDto> catalogList = addAgentProductBean.getCatalogList();
			
			catalogList.forEach(b->{
				b.setStock(10000);
				b.setDirectPrice(123.1);
				b.setVirtualStock(200);
				b.setWeight(20);
				b.setPic("http://1333");
				b.setLimitNum(123);
				b.setMarketAmount(123.4);
				b.setMarketPrice(321.5);
			});
//			addAgentProductBean.getCatalogList().add()
			updateproductskuBean.setCatalogList(catalogList);
			updateproductskuCall.setData(updateproductskuBean);
			updateproductskuCall.callService();
			Logger.verifyEquals("200", updateproductskuCall.getString("code"), "验证code");
			
			Thread.sleep(2000);
			AgentProductCallService.checkAddAgentProduct(productId, addAgentProductBean, false);
			System.out.println(addAgentProductBean.getCatalogList().get(0).getStock());
			verifyMongoCurMq(productId);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_UpdateProductSku_002() {
		Logger.start(true, "增加一个规格，成功");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(vipRootUserId,
					vipRootUserName, false);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			System.out.println(addAgentProductBean.getCatalogList().get(0).getStock());
			Thread.sleep(2000);
			updateproductskuBean.setUserId(vipRootUserId);
			updateproductskuBean.setProductId(productId);
			CatalogDto b = new CatalogDto();
			b.setStock(10000);
			b.setDirectPrice(123.1);
			b.setVirtualStock(200);
			b.setWeight(20);
			b.setPic("http://1333");
			b.setLimitNum(123);
			b.setMarketAmount(123.4);
			b.setMarketPrice(321.5);
			b.setSku("123123");
			b.setPropertyList(addAgentProductBean.getCatalogList().get(0).getPropertyList());
			b.setAgentPriceList(addAgentProductBean.getCatalogList().get(0).getAgentPriceList());
			addAgentProductBean.getCatalogList().add(b);
			updateproductskuBean.setCatalogList(addAgentProductBean.getCatalogList());
			updateproductskuCall.setData(updateproductskuBean);
			updateproductskuCall.callService();
			Logger.verifyEquals("200", updateproductskuCall.getString("code"), "验证code");
			Thread.sleep(2000);
			AgentProductCallService.checkAddAgentProduct(productId, addAgentProductBean, false);
			verifyMongoCurMq(productId);
			ProductDetailsDto selectProductDetails = AddAgentProductCallService.selectProductDetails(vipRootUserId, productId);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_UpdateProductSku_003() {
		Logger.start(true, "删除一个规格，成功");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(vipRootUserId,
					vipRootUserName, false);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			System.out.println(addAgentProductBean.getCatalogList().get(0).getStock());
			Thread.sleep(2000);
			updateproductskuBean.setUserId(vipRootUserId);
			updateproductskuBean.setProductId(productId);
			addAgentProductBean.getCatalogList().remove(0);
			updateproductskuBean.setCatalogList(addAgentProductBean.getCatalogList());
			updateproductskuCall.setData(updateproductskuBean);
			updateproductskuCall.callService();
			Logger.verifyEquals("200", updateproductskuCall.getString("code"), "验证code");
			Thread.sleep(2000);
			AgentProductCallService.checkAddAgentProduct(productId, addAgentProductBean, false);
			Logger.verifyEquals(addAgentProductBean.getCatalogList().size(),1 ,"删除了一个品类");
			
			verifyMongoCurMq(productId);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_UpdateProductSku_004() {
		Logger.start(true, "修改代理类型，成功");
		try {
			
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(vipRootUserId,
					vipRootUserName, true);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			System.out.println(addAgentProductBean.getCatalogList().get(0).getStock());
			Thread.sleep(2000);
			updateproductskuBean.setUserId(vipRootUserId);
			updateproductskuBean.setProductId(productId);
			List<CatalogDto> catalogList = addAgentProductBean.getCatalogList();
			
			
			catalogList.forEach(v->{
				v.getAgentPriceList().get(1).setAgentTypeName("上海代理");
				v.getAgentPriceList().get(1).setAgentPrice(153.3);
				v.getAgentPriceList().get(0).setAgentTypeName("北京代理");
				v.getAgentPriceList().get(0).setAgentPrice(123.3);
			});
			updateproductskuBean.setCatalogList(addAgentProductBean.getCatalogList());
			updateproductskuCall.setData(updateproductskuBean);
			updateproductskuCall.callService();
			Logger.verifyEquals("200", updateproductskuCall.getString("code"), "验证code");
			Thread.sleep(2000);
			AgentProductCallService.checkAddAgentProduct(productId, addAgentProductBean, false);
			verifyMongoCurMq(productId);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_UpdateProductSku_005() {
		Logger.start(true, "修改条形码，成功");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(vipRootUserId,
					vipRootUserName, false);
			List<String> barCodes = Arrays.asList("A123_b123_"+TestDataManager.getRandomNum(10),"A123_b123_"+TestDataManager.getRandomNum(10));
			addAgentProductBean.getCatalogList().get(0).setBarcode(barCodes.get(0));;
			addAgentProductBean.getCatalogList().get(1).setBarcode(barCodes.get(1));;
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			Thread.sleep(2000);
			updateproductskuBean.setUserId(vipRootUserId);
			updateproductskuBean.setProductId(productId);
			addAgentProductBean.getCatalogList().get(0).setBarcode(barCodes.get(0)+"_1");
			addAgentProductBean.getCatalogList().get(1).setBarcode(barCodes.get(1)+"_1");
			updateproductskuBean.setCatalogList(addAgentProductBean.getCatalogList());
			updateproductskuCall.setData(updateproductskuBean);
			updateproductskuCall.callService();
			Logger.verifyEquals("200", updateproductskuCall.getString("code"), "验证code");
			Thread.sleep(2000);
			
			YmtCatalogsIWapper ymtCatalogsIWapper = new YmtCatalogsIWapper();
			List<YmtCatalogsI> Catalogs = ymtCatalogsIWapper.selectCatalogsByProductId(productId);
			Logger.verifyEquals(Catalogs.get(0).getBarcode(), barCodes.get(0)+"_1", "验证条码");
			Logger.verifyEquals(Catalogs.get(1).getBarcode(), barCodes.get(1)+"_1", "验证条码");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_UpdateProductSku_006() {
		Logger.start(true, "验证es的pprice代理上级源商品修改直销价,刷新建议零售价");
		try {
			Integer mchId = GodmchManageCallService.CreateGodMch();
			Integer agenttype=GodmchManageCallService.getAgentType(mchId, 1);
			Integer agent_mchId = GodmchManageCallService.getAgentMchId(mchId, agenttype);
			
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(mchId,"", false);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			
			ImportAgentProductBean importAgentProductBean = AddAgentProductCallService.initImportAgentProduct(agent_mchId,"",productId,mchId,false);
			String agentProductId = AddAgentProductCallService.ImportAgentProduct(importAgentProductBean);
			
			Thread.sleep(2000);
			updateproductskuBean.setUserId(mchId);
			updateproductskuBean.setProductId(productId);
			List<CatalogDto> catalogList = addAgentProductBean.getCatalogList();
			
			catalogList.forEach(b->{
				b.setStock(10000);
				b.setDirectPrice(123.1);
				b.setVirtualStock(200);
				b.setWeight(20);
				b.setPic("http://1333");
				b.setLimitNum(123);
				b.setMarketAmount(123.4);
				b.setMarketPrice(321.5);
			});
//			addAgentProductBean.getCatalogList().add()
			updateproductskuBean.setCatalogList(catalogList);
			updateproductskuCall.setData(updateproductskuBean);
			updateproductskuCall.callService();
			Logger.verifyEquals("200", updateproductskuCall.getString("code"), "验证code");
			
			Thread.sleep(2000);
			AgentProductCallService.checkAddAgentProduct(productId, addAgentProductBean, false);
			System.out.println(addAgentProductBean.getCatalogList().get(0).getStock());
			verifyMongoCurMq(productId);
			Thread.sleep(2000);
			EsGodProduct product=AgentProductCallService.getAgentProductEs(agentProductId);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_UpdateProductSku_007() {
		Logger.start(true, "验证按条件自动加价,修改价格后代理自动加价-精确到分");
		try {
			Integer mchId = GodmchManageCallService.CreateGodMch();
			Integer agenttype=GodmchManageCallService.getAgentType(mchId, 1);
			Integer agent_mchId = GodmchManageCallService.getAgentMchId(mchId, agenttype);
			createDepotBean bean=GodmchManageCallService.createDepot(mchId);
	        List<mch_depot_info> selectDepotByDepotTitle = GodmchManageCallService.SelectDepotByDepotTitle(bean.getDepotTitle());
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(mchId, "", false);
			addAgentProductBean.setDepotId(selectDepotByDepotTitle.get(0).getDepotId());
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			int percentage =5;
			setConditionMarkupConfigBean setConditionMarkupConfigBean = GodmchManageCallService.initDefSetConditionMarkupConfig(agent_mchId, mchId, percentage, addAgentProductBean, 1);
			//精确到分
			setConditionMarkupConfigBean.setDecimalType(3);
			GodmchManageCallService.SetConditionMarkupConfigCall(setConditionMarkupConfigBean);
			Thread.sleep(2000);
			
			  List<ProductListDto> getProductLists2 = AddAgentProductCallService.GetProductLists(agent_mchId);
			  int redo=0;
			  while(getProductLists2.size()<1&&redo<3) {
				  Thread.sleep(5000);
				  redo++;
				  getProductLists2 = AddAgentProductCallService.GetProductLists(agent_mchId);
			  }
			ProductListDto agentProd = getProductLists2.get(0);
			//商品打标
			Logger.verifyEquals(agentProd.getAutoMarkupStatus(),1,"已经被打上自动加价的标");
			Thread.sleep(2000);
			
			updateproductskuBean.setUserId(mchId);
			updateproductskuBean.setProductId(productId);
			List<CatalogDto> catalogList = addAgentProductBean.getCatalogList();
			
			catalogList.forEach(b->{
				b.setStock(10000);
				b.setDirectPrice(123.1);
				b.setVirtualStock(200);
				b.setWeight(20);
				b.setPic("http://1333");
				b.setLimitNum(123);
				b.setMarketAmount(123.4);
				b.setMarketPrice(321.5);
			});
//			addAgentProductBean.getCatalogList().add()
			updateproductskuBean.setCatalogList(catalogList);
			updateproductskuCall.setData(updateproductskuBean);
			updateproductskuCall.callService();
			Logger.verifyEquals("200", updateproductskuCall.getString("code"), "验证code");
			
			Thread.sleep(2000);
			getProductLists2 = AddAgentProductCallService.GetProductLists(agent_mchId);
			ProductListDto agentProd2 = getProductLists2.get(0);
			//商品打标
			 redo=0;
			if(agentProd.getCatalogList().get(0).getDirectPrice().equals( agentProd2.getCatalogList().get(0).getDirectPrice() )) {
				Thread.sleep(5000);
				redo++;
				getProductLists2 = AddAgentProductCallService.GetProductLists(agent_mchId);
			}
			BigDecimal sourcedp = new BigDecimal(catalogList.get(0).getDirectPrice());
			BigDecimal up=new BigDecimal((percentage+100)).divide(new BigDecimal(100)).setScale(2);
			BigDecimal newdp = sourcedp.multiply(up).setScale(2,BigDecimal.ROUND_DOWN);
			Logger.verifyEquals(newdp, new BigDecimal(agentProd2.getCatalogList().get(0).getDirectPrice()).setScale(2,BigDecimal.ROUND_DOWN),"验证价格");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_UpdateProductSku_008() {
		Logger.start(true, "验证按条件自动加价,修改价格后代理自动加价-10位");
		try {
			Integer mchId = GodmchManageCallService.CreateGodMch();
			Integer agenttype=GodmchManageCallService.getAgentType(mchId, 1);
			Integer agent_mchId = GodmchManageCallService.getAgentMchId(mchId, agenttype);
			createDepotBean bean=GodmchManageCallService.createDepot(mchId);
	        List<mch_depot_info> selectDepotByDepotTitle = GodmchManageCallService.SelectDepotByDepotTitle(bean.getDepotTitle());
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(mchId, "", false);
			addAgentProductBean.setDepotId(selectDepotByDepotTitle.get(0).getDepotId());
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			int percentage =5;
			setConditionMarkupConfigBean setConditionMarkupConfigBean = GodmchManageCallService.initDefSetConditionMarkupConfig(agent_mchId, mchId, percentage, addAgentProductBean, 1);
			//精确到分
			setConditionMarkupConfigBean.setDecimalType(4);
			GodmchManageCallService.SetConditionMarkupConfigCall(setConditionMarkupConfigBean);
			Thread.sleep(2000);
			
			  List<ProductListDto> getProductLists2 = AddAgentProductCallService.GetProductLists(agent_mchId);
			  int redo=0;
			  while(getProductLists2.size()<1&&redo<3) {
				  Thread.sleep(5000);
				  redo++;
				  getProductLists2 = AddAgentProductCallService.GetProductLists(agent_mchId);
			  }
			ProductListDto agentProd = getProductLists2.get(0);
			//商品打标
			Logger.verifyEquals(agentProd.getAutoMarkupStatus(),1,"已经被打上自动加价的标");
			Thread.sleep(2000);
			
			updateproductskuBean.setUserId(mchId);
			updateproductskuBean.setProductId(productId);
			List<CatalogDto> catalogList = addAgentProductBean.getCatalogList();
			
			catalogList.forEach(b->{
				b.setStock(10000);
				b.setDirectPrice(123.1);
				b.setVirtualStock(200);
				b.setWeight(20);
				b.setPic("http://1333");
				b.setLimitNum(123);
				b.setMarketAmount(123.4);
				b.setMarketPrice(321.5);
			});
//			addAgentProductBean.getCatalogList().add()
			updateproductskuBean.setCatalogList(catalogList);
			updateproductskuCall.setData(updateproductskuBean);
			updateproductskuCall.callService();
			Logger.verifyEquals("200", updateproductskuCall.getString("code"), "验证code");
			
			Thread.sleep(2000);
			getProductLists2 = AddAgentProductCallService.GetProductLists(agent_mchId);
			ProductListDto agentProd2 = getProductLists2.get(0);
			//商品打标
			 redo=0;
			if(agentProd.getCatalogList().get(0).getDirectPrice().equals( agentProd2.getCatalogList().get(0).getDirectPrice() )) {
				Thread.sleep(5000);
				redo++;
				getProductLists2 = AddAgentProductCallService.GetProductLists(agent_mchId);
			}
			BigDecimal sourcedp = new BigDecimal(catalogList.get(0).getDirectPrice());
			BigDecimal up=new BigDecimal((percentage+100)).divide(new BigDecimal(100)).setScale(2);
			BigDecimal newdp = sourcedp.multiply(up).setScale(-1,BigDecimal.ROUND_DOWN);
			Logger.verifyEquals(newdp, new BigDecimal(agentProd2.getCatalogList().get(0).getDirectPrice()).setScale(-1,BigDecimal.ROUND_DOWN),"验证价格");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
}